package com.example.contactapp.providers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Groups;

import com.example.contactapp.objects.ContactObject;
import com.example.contactapp.views.SectionListItem;

public class ContactThread {
	private Context mContext;
	private static ContactThread instance;

	private ContactThread(Context context) {
		mContext = context;
	}

	public static ContactThread getInstance(Context context) {
		if (instance == null) {
			instance = new ContactThread(context);
		}
		return instance;
	}

	public ArrayList<SectionListItem> getContactsByGroupTitle(String groupTitle) {
		ArrayList<SectionListItem> myArrayContact = new ArrayList<SectionListItem>();
		ContactObject contact = null;
		String groupID = getGroupId(groupTitle);
		String[] cProjection = { Contacts.DISPLAY_NAME,
				GroupMembership.CONTACT_ID };
		Cursor groupCursor = mContext
				.getContentResolver()
				.query(Data.CONTENT_URI,
						cProjection,
						CommonDataKinds.GroupMembership.GROUP_ROW_ID
								+ "= ?"
								+ " AND "
								+ ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
								+ "'",
						new String[] { String.valueOf(groupID) }, null);
		if (groupCursor != null && groupCursor.moveToFirst()) {
			do {
				contact = new ContactObject();
				int nameCoumnIndex = groupCursor
						.getColumnIndex(Phone.DISPLAY_NAME);

				String name = groupCursor.getString(nameCoumnIndex);
				contact.setName(name);
				long contactId = groupCursor.getLong(groupCursor
						.getColumnIndex(GroupMembership.CONTACT_ID));

				Cursor numberCursor = mContext.getContentResolver().query(
						Phone.CONTENT_URI,
						new String[] { Phone.NUMBER, Phone.PHOTO_URI },
						Phone.CONTACT_ID + "=" + contactId, null, null);

				if (numberCursor.moveToFirst()) {
					int numberColumnIndex = numberCursor
							.getColumnIndex(Phone.NUMBER);
					do {
						String phoneNumber = numberCursor
								.getString(numberColumnIndex);
						contact.setAddress(phoneNumber);
						String avatar = numberCursor.getString(numberCursor
								.getColumnIndex(Phone.PHOTO_URI));
						contact.setAvatar(avatar);
					} while (numberCursor.moveToNext());
					numberCursor.close();
				} else {
				}
				myArrayContact.add(new SectionListItem(contact));
			} while (groupCursor.moveToNext());
			// groupCursor.close();
		} else {
		}

		Collections.sort(myArrayContact, new Comparator<SectionListItem>() {

			@Override
			public int compare(SectionListItem lhs, SectionListItem rhs) {
				return lhs.toString().compareTo(rhs.toString());
			}

		});

		return myArrayContact;
	}

	private String getGroupId(String groupTitle) {
		String selection = Groups.DELETED + "=? and " + Groups.GROUP_VISIBLE
				+ "=?";
		Cursor cursor = mContext.getContentResolver().query(Groups.CONTENT_URI,
				null, null, null, null);
		cursor.moveToFirst();
		int len = cursor.getCount();

		String groupId = null;
		for (int i = 0; i < len; i++) {
			String id = cursor.getString(cursor.getColumnIndex(Groups._ID));
			String title = cursor
					.getString(cursor.getColumnIndex(Groups.TITLE));

			if (title.equals(groupTitle)) {
				groupId = id;
				break;
			}
			cursor.moveToNext();
		}
		// cursor.close();

		return groupId;
	}
}
